import { defineStore } from 'pinia'
import api from '@/api'
import router from '@/router';

// 定义用户信息接口
interface TeacherInfo {
  teacherId: string;
  teacherName: string;
  telephone: string;
  role: string;
  imgPath: string;
  code: string;
  creatName: string;
  creatTime: string;
  amendName: string;
  amendTime: string;
}

export const TeacherInfo = defineStore('TeacherInfo', {
  state: () => {
    return {
      teacherinfo: {
        teacherId: '',
        teacherName: '',
        telephone: '',
        role: '',
        imgPath: '',
        code: '',
        creatName: '',
        creatTime: '',
        amendName: '',
        amendTime: '',
      }
    }
  },
  getters: {
    // 修正为访问 state.teacherinfo
    getTeacherId: (state) => state.teacherinfo.teacherId,
    getTeacherName: (state) => state.teacherinfo.teacherName,
    getTelephone: (state) => state.teacherinfo.telephone,
    getRole: (state) => state.teacherinfo.role,
    getImgPath: (state) => state.teacherinfo.imgPath,
  },
  actions: {
    // 修正为更新 teacherinfo
    setUserInfo(user: Partial<TeacherInfo>) {
      this.teacherinfo = { ...this.teacherinfo, ...user };
    },

    clearUserInfo() {
      this.teacherinfo = {
        teacherId: '',
        teacherName: '',
        telephone: '',
        role: '',
        imgPath: '',
        code: '',
        creatName: '',
        creatTime: '',
        amendName: '',
        amendTime: '',
      };
    },
  },
  persist: true, // 启用持久化
});
    // //外部用
    // async getUserInfo(user:any) {
    //   await api.userLogin.login(user).then(res => {
    //     console.log(res);
    //     if (res.code === "200") {
    //       this.userinfo = res.data
    //       // 登录成功后跳转到后台页面
    //       console.log(this.userinfo);
    //       // 直接跳转到后台页面 token
    //       localStorage.setItem('token', this.getToken);
    //       //  window.location.href = '/back';
    //       // 利用路由跳转
    //       router.push('/back');
    //     } else {
    //       alert('获取用户信息失败:', res.message)
    //       return false;
    //     }
    //   })
    // }

